package com.ruoyi.dm.component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.constant.CollectFileTypeConstants;
import com.ruoyi.common.utils.biz.BizModule;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.domain.po.AirCollectRecordPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 天基 大气温度 test 样例解析
 */
@Component
@Slf4j
public class TempTestDataStrategy implements DataStrategy<AirCollectRecordPO> {
    @Override
    public AirCollectRecordPO makeupPo(AirCollectRecordPO po) {
        //载荷名-数据级别-年和年积日-轨道数-数据版本.txt
        //test_temp_20020404.txt
        String name = po.getFileName();
        String timeStr = name.split("_|\\.")[2];
        LocalDate localDate = LocalDate.parse(timeStr, DateTimeFormatter.BASIC_ISO_DATE);
        po.setFileDate(localDate);
        return po;
    }


    @Override
    public String pythonCmd(String satellite, String dataType, String filePath, String method, Integer year) {
        String fileType = CollectFileTypeConstants.TYPE_TEMP;
        String name = new File(filePath).getName();
        //只有一个算法
        String outPath = PathUtil.outputModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") + "/" + fileType + "/" + satellite;
        FileUtil.mkdir(outPath);
        String logPath = PathUtil.tmpModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") + "/" + fileType + "/" + satellite;
        FileUtil.mkdir(logPath);
        String script = PathUtil.codeModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("csvScript");
        String[] cmdLines = new String[8];
        cmdLines[0] = "python";
        cmdLines[1] = script;
        cmdLines[2] = "-ip";
        cmdLines[3] = filePath;

        cmdLines[4] = "-op";
        cmdLines[5] = outPath+"/"+name;
        cmdLines[6] = "-lp";
        cmdLines[7] = outPath+"/"+name+".log";
        String commond = ArrayUtil.join(cmdLines, " ");
        log.info("拼接脚本:{}", commond);
        return commond;
    }
}
